<?php
/**
 * Description of MoneyOpsclass
 *
 * @author marius
 */

require_once ("Money.class.php");
require_once ("MoneyFactory.class.php");
require_once ("MoneyExceptions.php");

class MoneyOps {
	
	public static function add($m1,$m2)
	{
		if (!$m1->sameCurrency($m2)) {
			throw new IncompatiblesCurrenciesException("incompatible currencies");
		}

		$_factory=MoneyFactory::getDefaultFactory();

		return $_factory->createMoney(
			$m1->getValueNote() + $m2->getValueNote(),
			$m1->getValuePiece() + $m2->getValuePiece(),
			$m1->getCurrency());	
	}

	// enleve m2 à m1 si les valeurs sont suffisante
	public static function sub($m1, $m2)
	{
		if (!$m1->sameCurrency($m2)) {
			throw new IncompatiblesCurrenciesException("incompatible currencies");
		}
		if ( $m2->getValuePiece() > $m1->getValuePiece() ) {
			throw new InvalidMoneyValueException(" piece value too much");
		}
		if ( $m2->getValueNote() > $m1->getValueNote() ) {
			throw new InvalidMoneyValueException(" note value too much");
		}

		$_factory = MoneyFactory::getDefaultFactory();

		return $_factory->createMoney(
			$m1->getValueNote() - $m2->getValueNote(),
			$m1->getValuePiece() - $m2->getValuePiece(),
			$m2->getCurrency()
			);
	}
}
?>
